package br.com.fogas.medicao.activity;

import java.sql.SQLException;
import br.com.fogas.database.entidade.NotaOcorrencia;
import br.com.fogas.database.entidade.PlanoVisitas;
import br.com.fogas.database.enums.PlanoVisitaStatusEnum;
import br.com.fogas.medicao.R;
import br.com.fogas.utils.dialog.ProgressDialogBuilder;
import br.com.fogas.utils.task.ServiceTask;
import br.com.fogas.utils.task.ServiceTaskBusiness;
import br.com.fogas.medicao.activity.dialog.DialogObservacao;
import br.com.fogas.medicao.activity.dialog.DialogObservacao.DialogObservacaoListener;
import br.com.fogas.medicao.activity.helper.PlanoClientesHelper;
import br.com.fogas.medicao.application.Constants;
import br.com.fogas.medicao.business.LeituraQRCodeBusiness;
import br.com.fogas.medicao.business.VisitaSemConsumoBusiness;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem.OnMenuItemClickListener;

public class PlanoClientesFragment extends Fragment {
	
	private PlanoClientesHelper helper;

	private PlanoVisitas planoLocal;

	private LeituraQRCodeBusiness leituraQrCode;
	
	private NotaOcorrencia ultimaOcorrencia;
	
	public static final PlanoClientesFragment newInstance(PlanoVisitas planoVisitas) {
		Bundle bundle = new Bundle();
		bundle.putSerializable(Constants.PLANO_SELECIONADO, planoVisitas);
		PlanoClientesFragment fragment = new PlanoClientesFragment();
		fragment.setArguments(bundle);
		return fragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.planoLocal = (PlanoVisitas) getArguments().getSerializable(Constants.PLANO_SELECIONADO);
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.plano_clientes, container, false);
        return view;
    }
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		this.helper = new PlanoClientesHelper(this, getPlanoLocal());
		this.leituraQrCode = new LeituraQRCodeBusiness(helper);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		PlanoVisitas plano = (PlanoVisitas) helper.getPlanoSelecionado();
		
		menu.setHeaderTitle(plano.getCliente().getTx_nome());
		
		if(plano.getStatusEnum().equals(PlanoVisitaStatusEnum.PENDENTE)) {
			menu.add(R.string.menu_plano_faturar_consumo).setOnMenuItemClickListener(faturarConsumo);
			menu.add(R.string.menu_plano_visita_sem_consumo).setOnMenuItemClickListener(visitaSemConsumo);
			menu.add(R.string.menu_plano_atualizar_dados_cliente).setOnMenuItemClickListener(atualizarDadosCliente);
		}
		
		if(plano.getStatusEnum().equals(PlanoVisitaStatusEnum.FINALIZADO)) {
			menu.add(R.string.menu_plano_visita_sem_consumo).setOnMenuItemClickListener(visitaSemConsumo);
			menu.add(R.string.menu_plano_atualizar_dados_cliente).setOnMenuItemClickListener(atualizarDadosCliente);
		}
		
		if(plano.getStatusEnum().equals(PlanoVisitaStatusEnum.CANCELADO)) {
			menu.add(R.string.menu_plano_visita_sem_consumo).setOnMenuItemClickListener(visitaSemConsumo);
		}
		
		super.onCreateContextMenu(menu, v, menuInfo);
	}
	
	OnMenuItemClickListener faturarConsumo = new OnMenuItemClickListener() {
		
		@Override
		public boolean onMenuItemClick(MenuItem item) {
			leituraQrCode.lerQRCODE();
			return false;
		}
	};
	
	OnMenuItemClickListener visitaSemConsumo = new OnMenuItemClickListener() {
		
		@Override
		public boolean onMenuItemClick(MenuItem item) {
			visitaSemConsumo();
			return false;
		}
	};
	
	public void visitaSemConsumo() {
		try {
			ultimaOcorrencia = helper.getDatabase()
					.getNotaOcorrencia().ultimaOcorrenciaPorPlano(helper.getPlanoSelecionado());
			
			String observacao = "";
			if(ultimaOcorrencia != null) {
				observacao = ultimaOcorrencia.getTx_obs1() + ultimaOcorrencia.getTx_obs2();
			}
			
			DialogObservacaoListener observacaoListener = new DialogObservacaoListener() {
				
				@Override
				public void onConfirmarClick(String observacao) {
					
					ServiceTaskBusiness business = new VisitaSemConsumoBusiness(helper, observacao, ultimaOcorrencia);
					
					ProgressDialogBuilder dialog = new ProgressDialogBuilder(getActivity())
					.setMessage(getString(R.string.dialog_atualizar_ocorrencia));
					
					ServiceTask task = new ServiceTask(dialog, business);
					task.execute();
				}
			};
			
			DialogObservacao.newInstance(observacaoListener, observacao)
			.showDialog(getActivity().getSupportFragmentManager());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	OnMenuItemClickListener atualizarDadosCliente = new OnMenuItemClickListener() {
		
		@Override
		public boolean onMenuItemClick(MenuItem item) {
			
			return false;
		}
	};
	
	/*
	OnMenuItemClickListener finalizarLoja = new OnMenuItemClickListener() {
		
		@Override
		public boolean onMenuItemClick(MenuItem item) {
			try {
				AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
				PlanoVisitas plano = helper.getPlanos().get(info.position);
				
				plano.setStatus(PlanoVisitaStatusEnum.FINALIZADO.getId());
				helper.getDatabase().getPlanoVisitas().update(plano);
				helper.atualizarTela(plano.getShopping());
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return false;
		}
	};
	*/
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(LeituraQRCodeBusiness.REQUEST_CODE == requestCode) {
			this.leituraQrCode.onActivityResult(requestCode, resultCode, data);
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		registerForContextMenu(helper.getLvPlanoLojas());
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		helper.onDestroy();
	}
	
	public PlanoVisitas getPlanoLocal() {
		return planoLocal;
	}
	
	public PlanoClientesHelper getHelper() {
		return helper;
	}
}